package com.allwees.sender;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;

/**
 *
 *
 * @author yangylsky
 * @since 2019-10-09
 *
 */
@Lazy(false)
@Slf4j
public class AppSender {

    private String appKey;

    private String masterSecret;

	private static JPushClient jpushClient;

	public AppSender() {
	}

	public static void init(String appKey, String masterSecret) {
		jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
	}

	public void initConfig() {
		init(appKey, masterSecret);
	}

	public static void push(String pushId, String title, String text) {
		send(buildPushObject_android_and_ios(pushId, title, text));
	}

	public static void push(String title, String text) {
		send(buildPushObject_android_and_ios(title, text));
	}

	private static void send(PushPayload payload) {
		try {
			PushResult result = jpushClient.sendPush(payload);
			log.info("消息推送结果: " + result);
		} catch(APIConnectionException | APIRequestException e) {
			log.error("发送消息失败", e);
		}
	}

	private static PushPayload buildPushObject_android_and_ios(String pushId, String title, String text) {
		Notification notification = buildNotification(title, text);
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.registrationId(pushId))
                .setNotification(notification)
                .build();
    }

	private static PushPayload buildPushObject_android_and_ios(String title, String text) {
		Notification notification = buildNotification(title, text);
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setNotification(notification)
                .build();
    }

    private static Notification buildNotification(String title, String text) {
        IosAlert iosAlert = IosAlert.newBuilder()
                .setTitleAndBody(title, null, text)
                .build();

        return Notification.newBuilder()
                .addPlatformNotification(
                        IosNotification.newBuilder()
                                .setAlert(iosAlert)
                                .setBadge(1)
                                .build())
                .addPlatformNotification(
                        AndroidNotification.newBuilder()
                                .setTitle(title)
                                .setAlert(text)
                                .build())
                .build();
    }

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public void setMasterSecret(String masterSecret) {
		this.masterSecret = masterSecret;
	}
}
